# -*- coding: utf-8 -*-
"""
Created on Wed Dec  3 10:32:48 2014

@author: aaron
"""
import sqlite3
import hashlib
import re
import os
class md5check():
    def __init__(self,db_name):
        self.user = sqlite3.connect(db_name)
        if self.user == 0:
            print "db error"
            exit(0)
    def check(self,urn,pwd):
        user_cu = self.user.cursor()
        user_cu.execute("select password from user where username='%s'"%urn)
        try:
            if self.salt_get(urn,pwd)==user_cu.fetchall()[0][0]:
                print "secceful!!!"
                return 1
            else:
                print "password error!!!"
                return 0
        except IndexError:
            return 0
    def sqlcheck(self,string):
        pattern = re.compile(r'(\W+?)')
        match = pattern.findall(string)
        print match
        if match:
            print "error"
            return 1

    def encry(self,string):
        print string
        handle = hashlib.md5()
        handle.update(string.strip())
        return handle.hexdigest()
    def getinfo(self,urn):
        user_cu = self.user.cursor()
        user_cu.execute("select info from info where username='%s'"%urn)
        info = user_cu.fetchall()[0][0]
        return info
    def salt_get(self,urn,pwd):
        user_cu = self.user.cursor()
        user_cu.execute("select salt from user where username='%s'"%urn)
        salt = user_cu.fetchall()[0][0]
        mix = ""
        for x in range(32):
            mix = mix + pwd[x]+salt[x]
            print mix
        return self.encry(mix)
    def salt_mix(self,pwd,salt):
        mix = ""
        for x in range(32):
            mix = mix + pwd[x]+salt[x]
            print mix
        return self.encry(mix)
    def gen_salt(self):
        salt = os.urandom(24).encode("base64")[0:32]
        return salt
    def insert_user(self,urn,pwd,info):
        salt = self.gen_salt()
        print salt
        password = self.salt_mix(pwd,salt)
        print password
        user_cu = self.user.cursor()
        user_cu.execute("select username from user")
        #print user_cu.fetchall().count((u'qmaoao',))
        if user_cu.fetchall().count((urn.encode("utf8"),)) > 0:
            print "username has been used"
            return 0  
        else:     
            user_cu = self.user.cursor()
            user_cu.execute("insert into user values('%s','%s','%s')"%(urn,password,salt))
            self.inser_user_info(urn,info)
            self.user.commit()
            print "ok"
            return 1
    def delete_user(self,urn):
        user_cu = self.user.cursor()
        user_cu.execute("delete from user where username = '%s'"%urn)
        user_cu.execute("delete from info where username = '%s'"%urn)
        self.user.commit()
    def inser_user_info(self,urn,info):
        user_cu = self.user.cursor()
        user_cu.execute("insert into info values('%s','%s')"%(urn,info))
        self.user.commit()
if __name__ == '__main__':
    c = md5check("db/user.db")
    c.insert_user("qmaoao",c.encry("ouyang"),"I am so cool")
    #c.delete_user("qmaoao")    
        
    
        

        